const express = require('express');
const multer = require('multer');
const { askLongAI } = require('../ai/longAI');
const router = express.Router();
const upload = multer({ storage: multer.memoryStorage() });

router.post('/analyze', upload.none(), async (req, res) => {
    console.log('请求AI大模型分析');
    let { fileList, prompt } = req.body;
    if (typeof fileList === 'string') {
        fileList = JSON.parse(fileList);
    }
    console.log('文件列表：' + fileList.map((e) => e.fileName).join(', '));
    console.log('提示语：' + prompt);
    const completion = await askLongAI({ fileList, prompt });
    res.writeHead(200, {
        'content-type': 'text/plain; charset=utf-8',
    });
    for await (const chunk of completion) {
        const [choice] = chunk.choices;
        const { content } = choice.delta;
        console.log(content);
        res.write(content);
    }
    res.end();
});

module.exports = router;
